import {BaseElement} from "slate/dist/interfaces/element";
import {DomEditor, IDomEditor, SlateTransforms} from "@wangeditor/editor";
import {Path} from "slate";
import typeOfUtils from "../../utils/type-of-utils";

export default class WangeditorUtils {
    static replaceNode(oldNode: BaseElement, editor: IDomEditor, newNode: BaseElement | string): Path {
        let path = DomEditor.findPath(editor, oldNode)
        editor.select(path)
        SlateTransforms.removeNodes(editor, {at: path})
        if (typeOfUtils.isString(newNode)) {
            editor.insertText(newNode as string)
        } else {
            editor.insertNode(newNode as BaseElement)
        }
        return path;
    }
}